package com.inmobi.re.controller.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.drawable.BitmapDrawable;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Build.VERSION;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.MediaController;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import android.widget.VideoView;
import com.inmobi.commons.internal.Log;
import com.inmobi.commons.uid.UIDHelper;
import com.inmobi.re.container.IMWebView;
import com.inmobi.re.container.WrapperFunctions;
import com.inmobi.re.controller.JSController.Dimensions;
import com.inmobi.re.controller.JSController.PlayerProperties;
import java.lang.ref.WeakReference;

public class AVPlayer extends VideoView
  implements MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener, MediaPlayer.OnPreparedListener
{
  public static final int MINIMAL_LAYOUT_PARAM = 1;
  private static String f = "play";
  private static String g = "pause";
  private static String h = "ended";
  private static int i = -1;
  private static int j = 2;
  private static String k = "Loading. Please Wait..";
  private JSController.PlayerProperties a;
  private AVPlayerListener b;
  private String c;
  private int d = 0;
  private RelativeLayout e;
  private boolean l = false;
  private boolean m = false;
  private IMWebView n;
  private Bitmap o = null;
  private int p;
  public boolean pseudoPause = false;
  private int q = -1;
  private playerState r;
  private MediaPlayer s;
  private boolean t;
  private ViewGroup u;
  private JSController.Dimensions v;
  private a w = new a(this);

  private AVPlayer(Context paramContext)
  {
    super(paramContext);
  }

  public AVPlayer(Context paramContext, IMWebView paramIMWebView)
  {
    this(paramContext);
    setZOrderOnTop(true);
    this.r = playerState.INIT;
    setDrawingCacheEnabled(true);
    this.n = paramIMWebView;
    setFocusable(true);
    setFocusableInTouchMode(true);
    this.p = 100;
  }

  private static String a(String paramString)
  {
    try
    {
      byte[] arrayOfByte = paramString.getBytes();
      StringBuffer localStringBuffer = new StringBuffer();
      for (int i1 = 0; ; i1++)
        if (i1 < arrayOfByte.length)
        {
          if ((0x80 & arrayOfByte[i1]) > 0)
            localStringBuffer.append("%" + UIDHelper.byteToHex(arrayOfByte[i1]));
          else
            localStringBuffer.append((char)arrayOfByte[i1]);
        }
        else
        {
          String str = new String(localStringBuffer.toString().getBytes(), "ISO-8859-1");
          return str;
        }
    }
    catch (Exception localException)
    {
    }
    return null;
  }

  private void a()
  {
    if (this.a.showControl())
    {
      MediaController localMediaController = new MediaController(getContext());
      setMediaController(localMediaController);
      localMediaController.setAnchorView(this);
    }
  }

  private void a(int paramInt)
  {
    if (this.n != null)
      this.n.injectJavaScript("window.mraidview.fireMediaErrorEvent('" + this.a.id + "'," + paramInt + ");");
  }

  private void a(int paramInt1, int paramInt2)
  {
    if (this.n != null)
      this.n.injectJavaScript("window.mraidview.fireMediaTimeUpdateEvent('" + this.a.id + "'," + paramInt1 + "," + paramInt2 + ");");
  }

  private void a(boolean paramBoolean, int paramInt)
  {
    if (this.n != null)
    {
      this.n.injectJavaScript("window.mraidview.fireMediaCloseEvent('" + this.a.id + "'," + paramBoolean + "," + paramInt + ");");
      this.n.mediaPlayerReleased(this);
    }
  }

  private void b()
  {
    this.r = playerState.INIT;
    e();
    setVideoPath(this.c);
    a();
    setOnCompletionListener(this);
    setOnErrorListener(this);
    setOnPreparedListener(this);
  }

  private void b(int paramInt)
  {
    if ((this.s != null) && (this.t))
      this.t = false;
    this.p = paramInt;
    float f1 = 1.0F - (float)(Math.log(101 - paramInt) / Math.log(101.0D));
    try
    {
      this.s.setVolume(f1, f1);
      g();
      return;
    }
    catch (Exception localException)
    {
      while (true)
        this.n.raiseError("Player has been released. Cannot set the volume.", "setVolume");
    }
  }

  private void b(String paramString)
  {
    if (this.n != null)
      this.n.injectJavaScript("window.mraidview.fireMediaTrackingEvent('" + paramString + "','" + this.a.id + "');");
  }

  private void c()
  {
    if (this.r == playerState.SHOWING)
      if (this.m)
      {
        localplayerState = playerState.COMPLETED;
        this.r = localplayerState;
      }
    while ((!this.a.isAutoPlay()) || (this.r != playerState.INIT))
      while (true)
      {
        return;
        playerState localplayerState = playerState.PAUSED;
      }
    if (this.a.doMute())
      mute();
    start();
  }

  private void d()
  {
    try
    {
      ViewGroup localViewGroup1 = (ViewGroup)getParent();
      if (localViewGroup1 != null)
        localViewGroup1.removeView(this);
      ViewGroup localViewGroup2 = (ViewGroup)this.u.getParent();
      if (localViewGroup2 != null)
        localViewGroup2.removeView(this.u);
      setBackgroundColor(0);
      setBackgroundDrawable(null);
      setBackGroundLayout(null);
      return;
    }
    catch (Exception localException)
    {
      Log.internal("IMRE_3.7.0", "IMAVPlayer: unable to remove view");
    }
  }

  private void e()
  {
    this.e = new RelativeLayout(getContext());
    this.e.setLayoutParams(getLayoutParams());
    this.e.setBackgroundColor(-16777216);
    TextView localTextView = new TextView(getContext());
    localTextView.setText(k);
    localTextView.setTextColor(-1);
    RelativeLayout.LayoutParams localLayoutParams = new RelativeLayout.LayoutParams(-2, -2);
    localLayoutParams.addRule(13);
    this.e.addView(localTextView, localLayoutParams);
    ((ViewGroup)getParent()).addView(this.e);
  }

  private void f()
  {
    if (this.e != null)
      ((ViewGroup)getParent()).removeView(this.e);
  }

  private void g()
  {
    if (this.n != null)
      this.n.injectJavaScript("window.mraidview.fireMediaVolumeChangeEvent('" + this.a.id + "'," + getVolume() + "," + isMediaMuted() + ");");
  }

  private void h()
  {
    this.w.sendEmptyMessage(1001);
  }

  private void i()
  {
    this.w.removeMessages(1001);
  }

  private boolean j()
  {
    return (this.r == playerState.PAUSED) || (this.r == playerState.HIDDEN);
  }

  private boolean k()
  {
    return this.r == playerState.RELEASED;
  }

  private boolean l()
  {
    return this.r == playerState.PLAYING;
  }

  private boolean m()
  {
    return (getLayoutParams().width == 1) && (getLayoutParams().height == 1);
  }

  public boolean getAutoPlay()
  {
    if (this.a != null)
      return this.a.autoPlay;
    return false;
  }

  public ViewGroup getBackGroundLayout()
  {
    return this.u;
  }

  public String getMediaURL()
  {
    return this.c;
  }

  public JSController.Dimensions getPlayDimensions()
  {
    return this.v;
  }

  public JSController.PlayerProperties getPlayProperties()
  {
    return this.a;
  }

  public JSController.PlayerProperties getProperties()
  {
    return this.a;
  }

  public String getPropertyID()
  {
    return this.a.id;
  }

  public playerState getState()
  {
    return this.r;
  }

  public int getVolume()
  {
    return this.p;
  }

  public void hide()
  {
    try
    {
      setVisibility(4);
      this.u.setVisibility(4);
      this.r = playerState.HIDDEN;
      return;
    }
    catch (Exception localException)
    {
      Log.internal("IMRE_3.7.0", "IMAVPlayer: unable to hide video");
    }
  }

  public boolean isInlineVideo()
  {
    return !this.a.isFullScreen();
  }

  public boolean isMediaMuted()
  {
    return this.t;
  }

  public boolean isPrepared()
  {
    return this.l;
  }

  public void mute()
  {
    if ((this.s != null) && (!this.t))
      this.t = true;
    try
    {
      this.s.setVolume(0.0F, 0.0F);
      g();
      return;
    }
    catch (Exception localException)
    {
      while (true)
        Log.internal("IMRE_3.7.0", "IMAVPlayer: unable to set volume (mute)");
    }
  }

  public void onCompletion(MediaPlayer paramMediaPlayer)
  {
    Log.debug("IMRE_3.7.0", "AVPlayer-> onCompletion");
    this.r = playerState.COMPLETED;
    this.m = true;
    b(h);
    i();
    try
    {
      if (this.a.doLoop())
        try
        {
          if (!j())
          {
            this.d = 0;
            start();
          }
          return;
        }
        finally
        {
        }
    }
    catch (Exception localException)
    {
      Log.internal("IMRE_3.7.0", "IMAvplayer onCompletion exception ", localException);
      return;
    }
    if (this.a.exitOnComplete())
      releasePlayer(false);
  }

  public boolean onError(MediaPlayer paramMediaPlayer, int paramInt1, int paramInt2)
  {
    Log.debug("IMRE_3.7.0", "AVPlayer-> Player error : " + paramInt1);
    f();
    releasePlayer(false);
    if (this.b != null)
      this.b.onError(this);
    int i1 = i;
    if (paramInt1 == 100)
      i1 = j;
    a(i1);
    return false;
  }

  public void onIMVisibilityChanged(boolean paramBoolean)
  {
    Log.debug("IMRE_3.7.0", "AVPlayer-> onIMVisibilityChanged: " + paramBoolean);
    if ((paramBoolean) && (!this.pseudoPause))
      setBackgroundDrawable(new BitmapDrawable(this.o));
    if ((paramBoolean) && (this.pseudoPause))
    {
      this.pseudoPause = false;
      play();
    }
    if ((!paramBoolean) && (this.r == playerState.PLAYING))
    {
      this.pseudoPause = true;
      pause();
    }
  }

  public void onPrepared(MediaPlayer paramMediaPlayer)
  {
    this.s = paramMediaPlayer;
    super.seekTo(1000 * this.d);
    if (this.t);
    try
    {
      this.s.setVolume(0.0F, 0.0F);
      Log.debug("IMRE_3.7.0", "AVPlayer-> onPrepared");
      f();
      if (this.b != null)
        this.b.onPrepared(this);
      this.l = true;
      c();
      return;
    }
    catch (Exception localException)
    {
      while (true)
        Log.internal("IMRE_3.7.0", "IMAVPlayer: unable to set volume");
    }
  }

  protected void onVisibilityChanged(View paramView, int paramInt)
  {
    if (paramInt == 0);
    for (boolean bool = true; ; bool = false)
    {
      onIMVisibilityChanged(bool);
      return;
    }
  }

  protected void onWindowVisibilityChanged(int paramInt)
  {
    try
    {
      super.onWindowVisibilityChanged(paramInt);
      if (Build.VERSION.SDK_INT < 8)
        if (paramInt != 0)
          break label25;
      label25: for (boolean bool = true; ; bool = false)
      {
        onIMVisibilityChanged(bool);
        return;
      }
    }
    catch (Exception localException)
    {
      Log.internal("IMRE_3.7.0", "IMAVPlayer: onWindowVisibilityChanged: Something went wrong");
    }
  }

  public void pause()
  {
    try
    {
      if (this.r != null)
      {
        playerState localplayerState1 = this.r;
        playerState localplayerState2 = playerState.PAUSED;
        if (localplayerState1 != localplayerState2);
      }
      while (true)
      {
        return;
        super.pause();
        this.r = playerState.PAUSED;
        i();
        Log.debug("IMRE_3.7.0", "AVPlayer-> pause");
        b(g);
      }
    }
    finally
    {
    }
  }

  public void play()
  {
    b();
  }

  public void releasePlayer(boolean paramBoolean)
  {
    if (this.n != null)
      this.n.setOnTouchListener(new b());
    while (true)
    {
      try
      {
        if (k())
          return;
        this.r = playerState.RELEASED;
        if (this.q != -1)
        {
          i1 = this.q;
          a(paramBoolean, i1);
          i();
          stopPlayback();
          super.setMediaController(null);
          d();
          if (this.b == null)
            break;
          this.b.onComplete(this);
          this.b = null;
          return;
        }
      }
      finally
      {
      }
      int i1 = Math.round(getCurrentPosition() / 1000);
    }
  }

  public void seekPlayer(int paramInt)
  {
    if (paramInt <= getDuration())
      seekTo(paramInt);
  }

  public void setAutoPlay(boolean paramBoolean)
  {
    if (this.a != null)
      this.a.autoPlay = paramBoolean;
  }

  public void setBackGroundLayout(ViewGroup paramViewGroup)
  {
    this.u = paramViewGroup;
  }

  public void setListener(AVPlayerListener paramAVPlayerListener)
  {
    this.b = paramAVPlayerListener;
  }

  public void setPlayData(JSController.PlayerProperties paramPlayerProperties, String paramString)
  {
    this.a = paramPlayerProperties;
    this.c = paramString;
    if (paramPlayerProperties.audioMuted)
      this.t = true;
    this.c = this.c.trim();
    this.c = a(this.c);
    if (this.o != null);
    do
    {
      return;
      this.o = Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_8888);
    }
    while (Build.VERSION.SDK_INT < 8);
    try
    {
      this.o = WrapperFunctions.getVideoBitmap(this.c);
      return;
    }
    catch (Exception localException)
    {
      Log.internal("IMRE_3.7.0", "IMAVPlayer: unable to get video bitmap");
    }
  }

  public void setPlayDimensions(JSController.Dimensions paramDimensions)
  {
    this.v = paramDimensions;
  }

  public void setVolume(int paramInt)
  {
    if ((paramInt != this.p) && (this.r != playerState.RELEASED))
      b(paramInt);
  }

  public void show()
  {
    this.r = playerState.SHOWING;
    setVisibility(0);
  }

  public void start()
  {
    try
    {
      if ((this.n != null) && (!m()))
        this.n.setOnTouchListener(new c());
      if (this.r != null)
      {
        playerState localplayerState1 = this.r;
        playerState localplayerState2 = playerState.PLAYING;
        if (localplayerState1 != localplayerState2);
      }
      while (true)
      {
        return;
        seekPlayer(1000 * this.d);
        super.start();
        this.r = playerState.PLAYING;
        this.m = false;
        h();
        Log.debug("IMRE_3.7.0", "AVPlayer-> start playing");
        if (this.l)
          b(f);
      }
    }
    finally
    {
    }
  }

  public void unMute()
  {
    b(this.p);
  }

  static class a extends Handler
  {
    private final WeakReference<AVPlayer> a;

    public a(AVPlayer paramAVPlayer)
    {
      this.a = new WeakReference(paramAVPlayer);
    }

    public void handleMessage(Message paramMessage)
    {
      AVPlayer localAVPlayer = (AVPlayer)this.a.get();
      if (localAVPlayer != null)
        switch (paramMessage.what)
        {
        default:
        case 1001:
        }
      while (true)
      {
        super.handleMessage(paramMessage);
        do
          return;
        while (!AVPlayer.a(localAVPlayer));
        int i = Math.round(localAVPlayer.getCurrentPosition() / 1000);
        int j = Math.round(localAVPlayer.getDuration() / 1000);
        if (AVPlayer.b(localAVPlayer) != i)
        {
          AVPlayer.a(localAVPlayer, i, j);
          AVPlayer.a(localAVPlayer, i);
          AVPlayer.b(localAVPlayer, i);
        }
        sendEmptyMessageDelayed(1001, 1000L);
      }
    }
  }

  class b
    implements View.OnTouchListener
  {
    b()
    {
    }

    public boolean onTouch(View paramView, MotionEvent paramMotionEvent)
    {
      return false;
    }
  }

  class c
    implements View.OnTouchListener
  {
    c()
    {
    }

    public boolean onTouch(View paramView, MotionEvent paramMotionEvent)
    {
      return paramMotionEvent.getAction() == 2;
    }
  }

  public static enum playerState
  {
    static
    {
      PAUSED = new playerState("PAUSED", 2);
      HIDDEN = new playerState("HIDDEN", 3);
      SHOWING = new playerState("SHOWING", 4);
      COMPLETED = new playerState("COMPLETED", 5);
      RELEASED = new playerState("RELEASED", 6);
      playerState[] arrayOfplayerState = new playerState[7];
      arrayOfplayerState[0] = INIT;
      arrayOfplayerState[1] = PLAYING;
      arrayOfplayerState[2] = PAUSED;
      arrayOfplayerState[3] = HIDDEN;
      arrayOfplayerState[4] = SHOWING;
      arrayOfplayerState[5] = COMPLETED;
      arrayOfplayerState[6] = RELEASED;
    }
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.inmobi.re.controller.util.AVPlayer
 * JD-Core Version:    0.6.2
 */